package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.oam.domain.*;
import com.nt.oam.vo.asset.AccessDeviceAssetVo;
import com.nt.oam.vo.asset.DatabaseAssetVo;
import com.nt.oam.vo.asset.HardwareAssetVo;
import com.nt.oam.vo.asset.SoftwareAssetVo;
import com.nt.oam.vo.project.ProjectDetailsVo;
import com.nt.oam.vo.record.AllMessageVo;
import com.nt.oam.vo.record.OamOtherMessageVo;
import com.nt.oam.vo.record.OamRecordDetailVo;
import com.nt.oam.vo.server.ServerMessageVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntHospitalOamRecordMapper;
import com.nt.oam.service.IAntHospitalOamRecordService;

/**
 * 运维记录Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntHospitalOamRecordServiceImpl extends ServiceImpl<AntHospitalOamRecordMapper, AntHospitalOamRecord> implements IAntHospitalOamRecordService {
    private final AntHospitalOamRecordMapper antHospitalOamRecordMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;
    private final AntServerServiceImpl antServerService;
    private final AntSoftwareAssetServiceImpl antSoftwareAssetService;
    private final AntHardwareAssetServiceImpl antHardwareAssetService;
    private final AntDatabaseAssetServiceImpl antDatabaseAssetService;
    private final AntAccessDeviceAssetServiceImpl antAccessDeviceAssetService;
    private final SysUserServiceImpl sysUserService;

    /**
     * 查询运维记录
     *
     * @param oamRecordId 运维记录主键
     * @return 运维记录
     */
    @Override
    public AntHospitalOamRecord selectAntHospitalOamRecordByOamRecordId(Long oamRecordId) {
        return antHospitalOamRecordMapper.selectAntHospitalOamRecordByOamRecordId(oamRecordId);
    }

    /**
     * 查询运维记录列表
     *
     * @param antHospitalOamRecord 运维记录
     * @return 运维记录
     */
    @Override
    public List<AntHospitalOamRecord> selectAntHospitalOamRecordList(AntHospitalOamRecord antHospitalOamRecord) {
        return antHospitalOamRecordMapper.selectAntHospitalOamRecordList(antHospitalOamRecord);
    }

    /**
     * 新增运维记录
     *
     * @param antHospitalOamRecord 运维记录
     * @return 结果
     */
    @Override
    public int insertAntHospitalOamRecord(AntHospitalOamRecord antHospitalOamRecord) {
        antHospitalOamRecord.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHospitalOamRecord.getHospitalId();
        antHospitalOamRecord.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHospitalOamRecord.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antHospitalOamRecord.getProjectId();
        antHospitalOamRecord.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antHospitalOamRecord.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospitalOamRecord.setCreatePerson(sysUser.getUserName());
        }
        antHospitalOamRecord.setIsDeleted(YesOrNoEnum.NO.getCode());
        antHospitalOamRecord.setIsModify(YesOrNoEnum.NO.getCode());
        return antHospitalOamRecordMapper.insertAntHospitalOamRecord(antHospitalOamRecord);
    }

    /**
     * 修改运维记录
     *
     * @param antHospitalOamRecord 运维记录
     * @return 结果
     */
    @Override
    public int updateAntHospitalOamRecord(AntHospitalOamRecord antHospitalOamRecord) {
        antHospitalOamRecord.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHospitalOamRecord.getHospitalId();
        antHospitalOamRecord.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHospitalOamRecord.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antHospitalOamRecord.getProjectId();
        antHospitalOamRecord.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antHospitalOamRecord.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospitalOamRecord.setUpdatePerson(sysUser.getUserName());
        }
        return antHospitalOamRecordMapper.updateAntHospitalOamRecord(antHospitalOamRecord);
    }

    /**
     * 批量删除运维记录
     *
     * @param oamRecordIds 需要删除的运维记录主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalOamRecordByOamRecordIds(Long[] oamRecordIds) {
        List<Long> oamRecordIdsList = Arrays.asList(oamRecordIds);
        oamRecordIdsList.forEach(a -> {
            AntHospitalOamRecord antHospitalOamRecord = antHospitalOamRecordMapper.selectAntHospitalOamRecordByOamRecordId(a);
            antHospitalOamRecord.setIsDeleted(YesOrNoEnum.YES.getCode());
            antHospitalOamRecordMapper.updateAntHospitalOamRecord(antHospitalOamRecord);
        });
        //antHospitalOamRecordMapper.deleteAntHospitalOamRecordByOamRecordIds(oamRecordIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除运维记录信息
     *
     * @param oamRecordId 运维记录主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalOamRecordByOamRecordId(Long oamRecordId) {
        return antHospitalOamRecordMapper.deleteAntHospitalOamRecordByOamRecordId(oamRecordId);
    }

    /**
     * 查询运维记录详情信息
     *
     * @param oamRecordId 运维记录主键
     * @return 运维记录
     */
    @Override
    public AntHospitalOamRecord selectOamRecordDeatilVo(Long oamRecordId) {
        AntHospitalOamRecord record = antHospitalOamRecordMapper.selectAntHospitalOamRecordByOamRecordId(oamRecordId);
        if (Objects.nonNull(record)) {
            //医院名称
            AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(record.getHospitalId());
            if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                record.setHospitalName(antHospital.getHospitalName());
            }
            // 项目名称
            AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(record.getProjectId());
            if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                record.setProjectName(antHospitalProject.getProjectName());
            }
        }
        return record;
    }


    /**
     * 运维记录其他信息：资产信息，服务器信息
     */
    @Override
    public OamOtherMessageVo selectOamOtherMessage(Long projectId) {
        // 目标对象
        OamOtherMessageVo oamOtherMessageVo = new OamOtherMessageVo();
        // 服务器信息
        ServerMessageVo serverMessageVo = antServerService.selectByProjectIdServerMessage(projectId);
        oamOtherMessageVo.setServerMessageVo(serverMessageVo);
        // 软件资产信息(接口)
        List<SoftwareAssetVo> softwareAssetVo = antSoftwareAssetService.selectByProjectIdSoftwareAsset(projectId);
        oamOtherMessageVo.setSoftwareAssetVo(softwareAssetVo);
        // 硬件资产信息
        List<HardwareAssetVo> hardwareAssetVo = antHardwareAssetService.selectByProjectIdHardwareAsset(projectId);
        oamOtherMessageVo.setHardwareAssetVo(hardwareAssetVo);
        // 数据库资产信息
        List<DatabaseAssetVo> databaseAssetVo = antDatabaseAssetService.selectByProjectIdDatabaseAsset(projectId);
        oamOtherMessageVo.setDatabaseAssetVo(databaseAssetVo);
        // 接入设备资产信息
        List<AccessDeviceAssetVo> accessDeviceAssetVo = antAccessDeviceAssetService.selectByProjectIdAccessDeviceAsset(projectId);
        oamOtherMessageVo.setAccessDeviceAssetVo(accessDeviceAssetVo);
        //返回数据
        return oamOtherMessageVo;
    }

    /**
     * 所有信息
     * @param hospitalId
     * @return
     */
    @Override
    public AllMessageVo selectAllMessage(Long hospitalId){
        // 目标对象
        AllMessageVo allMessageVo = new AllMessageVo();
        // 项目信息
        List<ProjectDetailsVo> projectDetailsVo = new ArrayList<>();
        allMessageVo.setProjectDetailsVoList(projectDetailsVo);
        // 服务器信息
        ServerMessageVo serverMessageVo = antServerService.selectByHospitalIdServerMessage(hospitalId);
        allMessageVo.setServerMessageVo(serverMessageVo);
        // 软件资产信息(接口)
        List<SoftwareAssetVo> softwareAssetVo = antSoftwareAssetService.selectByHospitalIdSoftwareAsset(hospitalId);
        allMessageVo.setSoftwareAssetVo(softwareAssetVo);
        // 硬件资产信息
        List<HardwareAssetVo> hardwareAssetVo = antHardwareAssetService.selectByHospitalIdHardwareAsset(hospitalId);
        allMessageVo.setHardwareAssetVo(hardwareAssetVo);
        // 数据库资产信息
        List<DatabaseAssetVo> databaseAssetVo = antDatabaseAssetService.selectByHospitalIdDatabaseAsset(hospitalId);
        allMessageVo.setDatabaseAssetVo(databaseAssetVo);
        // 接入设备资产信息
        List<AccessDeviceAssetVo> accessDeviceAssetVo = antAccessDeviceAssetService.selectByHospitalIdAccessDeviceAsset(hospitalId);
        allMessageVo.setAccessDeviceAssetVo(accessDeviceAssetVo);
        //返回数据
        return allMessageVo;
    }


    /**
     * 查询是否在修改
     */
    public int selectRecordModify(Long recordId) {
        AntHospitalOamRecord antHospitalOamRecord = this.getById(recordId);
        int isModify = 0;
        if (Objects.nonNull(antHospitalOamRecord) && Objects.nonNull(antHospitalOamRecord.getIsModify())) {
            isModify = antHospitalOamRecord.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateRecordModify(Long recordId, Integer isModify) {
        AntHospitalOamRecord antHospitalOamRecord = this.getById(recordId);
        if (Objects.nonNull(antHospitalOamRecord)) {
            antHospitalOamRecord.setIsModify(isModify);
        }
        return antHospitalOamRecordMapper.updateAntHospitalOamRecord(antHospitalOamRecord) ;
    }
}
